package com.interesting.client.UI.video;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.interesting.R;
import com.interesting.base.core.BaseFragment;
import com.interesting.base.http.HttpUtil;
import com.interesting.client.adapter.VideoListAdapter;
import com.interesting.client.bean.VideoInfo;

public class VideoFragment extends BaseFragment implements OnItemClickListener {

	private GridView gridView;
	private Button video;
	private List<VideoInfo> dataList;
	private VideoListAdapter videoAdapter;
	private int currentPage = 1;
	private int totalPage;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.video_video_fragment, container, false);
		gridView = (GridView) v.findViewById(R.id.video_fragment_gridview);
		video = (Button) v.findViewById(R.id.video_fragment_video);
		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		dataList = new ArrayList<VideoInfo>();
		videoAdapter = new VideoListAdapter(getSherlockActivity(), dataList);
		gridView.setAdapter(videoAdapter);
		if (savedInstanceState == null) {
			new VideoListAsync().execute("1");
		}
		video.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new VideoListAsync().execute(currentPage % totalPage + "");

				// if (v == video) {
				// Intent intent = new Intent(getSherlockActivity(),
				// VideoActivity.class);
				// ((VideoAndMusicActivity)
				// getSherlockActivity()).ToActivity(intent);
				// }
			}
		});
		gridView.setOnItemClickListener(this);

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
		String url = dataList.get(position).getVpath();
		Intent intent = new Intent(getSherlockActivity(), VideoActivity.class);
		intent.putExtra("url", url);
		((VideoAndMusicActivity) getSherlockActivity()).ToActivity(intent);

	}

	/** 获取视频列表 */
	private class VideoListAsync extends AsyncTask<String, Void, JSONObject> {

		@Override
		protected JSONObject doInBackground(String... params) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("act", "videoList");
			map.put("type", "1");
			map.put("currentPage", params[0]);
			return HttpUtil.parseResult(HttpUtil.post(map));
		}

		@Override
		protected void onPostExecute(JSONObject result) {
			if (result != null) {
				try {
					Gson gson = new Gson();
					List<VideoInfo> data = gson.fromJson(result.getString("dataList"),
							new TypeToken<List<VideoInfo>>() {
							}.getType());
					for (int i = 0; i < dataList.size(); i++) {
						dataList.add(data.get(i));
					}
					currentPage = result.getInt("currentPage");
					totalPage = result.getInt("totalPage");
				} catch (JSONException e) {
					e.printStackTrace();
				}
				videoAdapter.notifyDataSetChanged();
			}

			super.onPostExecute(result);
		}

	}
}
